package com.scwatch.core.service.impl;

import com.scwatch.common.orm.SearchFilter;
import com.scwatch.core.domain.GaoCeng;
import com.scwatch.core.domain.Info;
import com.scwatch.core.domain.Special;
import com.scwatch.core.repository.GcDao;
import com.scwatch.core.repository.InfoDao;
import com.scwatch.core.repository.SpecialDao;
import com.scwatch.core.service.GcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Map;

/**
 * @author mafei
 * @Title: GcServiceImpl
 * @ProjectName SCGC_CMS
 * @Description: TODO
 * @date 2018/10/1515:21
 */
@Service
@Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
public class GcServiceImpl implements GcService {

    @Autowired
    private GcDao dao;

    @Autowired
    private InfoDao infoDao;

    @Autowired
    private SpecialDao specialDao;

    @Autowired
    public void setDao(GcDao dao) {
        this.dao = dao;
    }

    @Override
    public Page<GaoCeng> findAll(Map<String, String[]> params, Pageable pageable) {
        return dao.findAll(spec(params), pageable);
    }

    @Transactional
    public GaoCeng save(GaoCeng bean, Integer specialId) {
        String relate[]  = bean.getPersonRelateNews().replaceAll("\\{","").replaceAll("\\}","").replaceAll("\"","").split(":");
        Info info = infoDao.findOne(Integer.parseInt(relate[0]));
        bean.setInfo(info);
        Special special =specialDao.findOne(specialId);
        bean.setSpecial(special);
        bean.setPriority(0);
        return dao.save(bean);
    }

    @Transactional
    public GaoCeng update(GaoCeng bean,Integer specialId) {
        GaoCeng temp = dao.findOne(bean.getId());
        String relate[]  = bean.getPersonRelateNews().replaceAll("\\{","").replaceAll("\\}","").replaceAll("\"","").split(":");
        Info info = infoDao.findOne(Integer.parseInt(relate[0]));
        bean.setInfo(info);
        Special special =specialDao.findOne(specialId);
        bean.setSpecial(special);
        bean.setPriority(temp.getPriority());
        return dao.save(bean);
    }

    @Transactional
    public GaoCeng[] delete(Integer[] ids) {
        GaoCeng[] beans = new GaoCeng[ids.length];
        for (int i = 0; i < ids.length; i++) {
            beans[i] = delete(ids[i]);
        }
        return beans;
    }

    @Transactional
    public GaoCeng delete(Integer id) {
        GaoCeng entity = dao.findOne(id);
        dao.delete(entity);
        return entity;
    }

    @Transactional
    public GaoCeng getById(Integer id) {
        return dao.findOne(id);
    }

    @Transactional
    public void updateOrder(Integer order, Integer id) {
        dao.updateOrder(order,id);
    }

    private Specification<GaoCeng> spec(Map<String, String[]> params) {
        Collection<SearchFilter> filters = SearchFilter.parse(params).values();
        Specification<GaoCeng> sp = SearchFilter.spec(filters, GaoCeng.class);
        return sp;
    }
}
